#include <iostream>
#include <vector>
#include <queue>
using namespace std;
const int N = 1e4 + 10,INF = 2147483647;
vector<pair<int,int>> edges[N];
int dist[N];
bool st[N];
int n,m,s;

void spfa()
{
    for(int i = 1;i <= n; i++)dist[i] = INF;
    queue<int> q;
    dist[s] = 0;
    q.push(s);
    st[s] = true;
    while(q.size())
    {
        int t = q.front();
        q.pop();
        st[t] = false;
        for(auto& e : edges[t])
        {
            int v = e.first,w=e.second;
            if(dist[t]+w < dist[v])
            {
                dist[v] = dist[t] + w;
                if(st[v] ==false)
                {
                    q.push(v);
                    st[v] = true;
                }
            }
        }
    }

    for(int i = 1; i <= n; i++)
    {
        cout << dist[i] << " ";
    }
}

int main()
{
    cin >> n >> m >> s;
    for(int i = 1; i <= m; i++)
    {
        int u,v,w;
        cin >> u >> v >> w;
        edges[u].push_back({v,w});
    }  
    spfa();
    return 0;
}
